#import "PAStringListInspector.h"
#import <appkit/appkit.h>
#import "PAStringList.h"

@implementation PAStringListInspector

- init
{
    char buf[MAXPATHLEN + 1];
    id bundle;
    
    [super init];
    
    bundle = [NXBundle bundleForClass:[PAStringList class]];
    [bundle getPath:buf forResource:"PAStringListInspector" ofType:"nib"];
    [NXApp loadNibFile:buf owner:self withNames:NO fromZone:[self zone]];
	[stringText setDelegate:self];
    return self;
}

- textDidGetKeys:sender isEmpty:(BOOL)flag 
{ [self perform:@selector(ok:) with:self afterDelay:500 cancelPrevious:YES]; return self; }

- (BOOL)wantsButtons { return NO; }

- revert:sender
{
	int i;
	NXStream *s = NXOpenMemory(NULL, 0, NX_READWRITE);
	
	// Add strings to text object
	for(i=0; i<[object count]; i++) NXPrintf(s,"%s\n", [object stringAt:i]);
	NXSeek(s, 0, NX_FROMSTART);
	[stringText readText:s];
	NXCloseMemory(s, NX_FREEBUFFER);
		
	return [super revert:sender];
}

- ok:sender
{
	char *buffer;

	[object empty];

	// Add all of the strings from text object
	buffer = malloc([stringText textLength]*(sizeof(char)+1));
	[stringText getSubstring:buffer start:0 length:[stringText textLength]+ 1];
	[object addDelimitedStrings:buffer delimiters:"\n"];	
	free(buffer);
	
	return [super ok:sender];
}

@end
